package com.jintu.portal.service.datadriver;

import com.alibaba.fastjson.JSONObject;
import com.jintu.portal.config.SysConfigurer;
import com.jintu.portal.pojo.submit.DataRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author very
 */
@Component
public class CompleteRequestHelper {

    @Resource(name = "sysConfigurer")
    private SysConfigurer sysConfigurer;

    void completeRequestParameter(JSONObject merchPrivate, DataRequest dataRequest){
        if(dataRequest.getRequestJson() == null){
            dataRequest.setRequestJson(new JSONObject());
        }
        if(isBelongCarrierDataSource(dataRequest.getDataSourceCode())){
            merchPrivate.put("mobile", dataRequest.getRequestJson().getString("mobile"));
            merchPrivate.put("idCard", dataRequest.getRequestJson().getString("idCard"));
            merchPrivate.put("realName", dataRequest.getRequestJson().getString("realName"));
            dataRequest.getRequestJson().put("callBackUrl", sysConfigurer.getString("carrierCallbackUrl", "http://localhost:9092/carrierReceiver"));
        }
        if(isBelongZMScoreDataSource(dataRequest.getDataSourceCode())){
            dataRequest.getRequestJson().put("callBackUrl", sysConfigurer.getString("zmScoreCallbackUrl", "http://localhost:9092/zmScoreReceiver"));
        }
        if(isBelongTaobaoDataSource(dataRequest.getDataSourceCode())){
            dataRequest.getRequestJson().put("callBackUrl", sysConfigurer.getString("taobaoCallbackUrl", "http://localhost:9092/taobaoReceiver"));
        }
        if(isBelongGongJiJinDataSource(dataRequest.getDataSourceCode())){
            dataRequest.getRequestJson().put("callBackUrl", sysConfigurer.getString("gongJiJinCallbackUrl", "http://localhost:9092/gongJiJinReceiver"));
        }
        if(isBelongXueXinDataSource(dataRequest.getDataSourceCode())){
            dataRequest.getRequestJson().put("callBackUrl", sysConfigurer.getString("xueXinCallbackUrl", "http://localhost:9092/xueXinReceiver"));
        }
        if(isBelongSheBaoDataSource(dataRequest.getDataSourceCode())){
            dataRequest.getRequestJson().put("callBackUrl", sysConfigurer.getString("sheBaoCallbackUrl", "http://localhost:9092/sheBaoReceiver"));
        }
        if(isBelongWangYinDataSource(dataRequest.getDataSourceCode())){
            dataRequest.getRequestJson().put("callBackUrl", sysConfigurer.getString("wangYinCallbackUrl", "http://localhost:9092/wangYinReceiver"));
        }
        if(isBelongZhiFuBaoDataSource(dataRequest.getDataSourceCode())){
            dataRequest.getRequestJson().put("callBackUrl", sysConfigurer.getString("zhiFuBaoCallbackUrl", "http://localhost:9092/zhiFuBaoReceiver"));
        }
    }

    private boolean isBelongCarrierDataSource(String dataSourceCode) {
        if(StringUtils.isBlank(dataSourceCode)){
            return false;
        }
        return "carrier".equals(dataSourceCode);
    }

    private boolean isBelongZMScoreDataSource(String dataSourceCode) {
        if(StringUtils.isBlank(dataSourceCode)){
            return false;
        }
        return "zmScore".equals(dataSourceCode);
    }

    private boolean isBelongTaobaoDataSource(String dataSourceCode){
        if(StringUtils.isBlank(dataSourceCode)){
            return false;
        }
        return "taoBao".equals(dataSourceCode) || "taoBaoSelf".equals(dataSourceCode) || "taoBaoPartner".equals(dataSourceCode);
    }

    private boolean isBelongGongJiJinDataSource(String dataSourceCode) {
        if(StringUtils.isBlank(dataSourceCode)){
            return false;
        }
        return "gongJiJin".equals(dataSourceCode);
    }

    private boolean isBelongSheBaoDataSource(String dataSourceCode) {
        if(StringUtils.isBlank(dataSourceCode)){
            return false;
        }
        return "shebao".equals(dataSourceCode);
    }

    private boolean isBelongWangYinDataSource(String dataSourceCode) {
        if(StringUtils.isBlank(dataSourceCode)){
            return false;
        }
        return "wangyin".equals(dataSourceCode);
    }

    private boolean isBelongXueXinDataSource(String dataSourceCode) {
        if(StringUtils.isBlank(dataSourceCode)){
            return false;
        }
        return "xuexin".equals(dataSourceCode);
    }

    private boolean isBelongZhiFuBaoDataSource(String dataSourceCode) {
        if(StringUtils.isBlank(dataSourceCode)){
            return false;
        }
        return "zhifubao".equals(dataSourceCode);
    }
}
